<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Datas\System;

class Ip extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ip:screen';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '筛选国内ip';

    const CHINA_IP_HASH = System::CHINA_IP_HASH;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    public function handle(){
        $this->set_ip_list(env('CHINA_IP'));
    }

    function set_ip_list($ip_file) {
        //从文件中得到所有的国内ip
        $arr_all = file($ip_file);

        //遍历，得到所有的第一段
        $arr_first = array();
        foreach ($arr_all as $k => $rangeone) {
            $rangeone = trim($rangeone);
            if ($rangeone == "") {
                continue;
            }
            
            $first = explode(".", $rangeone);
            if (isset($first[0]) && $first[0]!='') {
               $arr_first[] = $first[0];
            }
        }

        //对所有的第一段去除重复
        $arr_first = array_unique($arr_first);

        //得到线上hash的所有key
        $arr_hkeys = $this->hash_keys(self::CHINA_IP_HASH);

        //如果一个线上已存在的key不再存在于新ip的第一段的数组中
        //需要从线上hash中删除
        if (is_array($arr_hkeys) && sizeof($arr_hkeys)>0) {
            foreach($arr_hkeys as $k => $hkey_one) {
               if (!in_array($hkey_one, $arr_first)) {
                    echo "will delete :".$hkey_one."\n";
                    $this->hash_delete_hkey(self::CHINA_IP_HASH,$hkey_one);
                }
            }
        }

        //得到每个第一段下面对应的所有ip地址段,保存到redis
        foreach ($arr_first as $k => $first) {
           $this->add_a_list_by_first($first,$arr_all);  
        }

    }


    //把所有的第一段为指定数字的ip,添加到redis
    function add_a_list_by_first($first,$arr) {

         $arr_line = array();
         foreach ($arr as $k => $rangeone) {
              $rangeone = trim($rangeone);
              $first_a = explode(".", $rangeone);
              if (!isset($first_a[0]) || $first_a[0] == "") {
                 continue;
              }
              $cur_first = $first_a[0];
              if ($cur_first == $first) {

                  $line = $this->get_line_by_rangeone($rangeone);
                  //echo "line:".$line."\n";
                  $arr_line[] = $line;
              } else {
                 continue;
              }
          }


          if (sizeof($arr_line) >0) {
              $key_name = $first;
              $this->hash_set(self::CHINA_IP_HASH,$key_name,$arr_line);
          }
    }


    //得到一个ip地址段的起始范围
    function get_line_by_rangeone($networkRange) {

            $s = explode('/', $networkRange);
            $network_start = (double) (sprintf("%u", ip2long($s[0])));
            $network_len = pow(2, 32 - $s[1]);
            $network_end = $network_start + $network_len - 1;

            $line = $network_start."--".$network_end;
            return $line;
    }

    //redis set 一个数组到hash
    function hash_set($hash_name,$key_name,$arr_value){
          $str_value = json_encode($arr_value);
          $b = Redis::hset($hash_name, $key_name, $str_value);
    }

    //返回redis hash中
    function hash_keys($hash_name) {
          $arr = Redis::hKeys($hash_name);
          return $arr;
    }

    //删除一个hash的hkey
    function hash_delete_hkey($hash_name,$key_name) {
          Redis::hdel($hash_name, $key_name);
    }
}
